<?php require_once 'header.php'; ?>
    <script language="Javascript" type="text/javascript">
    function showExp(y, m, d) {
        window.location = "index.php?year=" + y + "&month=" + m + "&date=" + d;
    }
    </script>

<?php
	require_once 'config.php' ;
	require_once 'Date.php';


	if(!empty($_REQUEST["month"])) {
	    $month = (int)$_REQUEST["month"];
    }
    else {
        $month = date('m');
        $month = (int)$month;
    }
	if(!empty($_REQUEST["year"])) {
	    $year = (int)$_REQUEST["year"];
    }
    else {
        $year = date('Y');
        $year = (int)$year;
    }
///date calculation.
    $nextMonth = $month + 1;
    $nextYear = $year;

    if($nextMonth == 13) {
        $nextMonth = 1;
        $nextYear ++;
    }
    $prevMonth = $month - 1;
    $prevYear = $year;
    if($prevMonth == 0) {
        $prevMonth = 12;
        $prevYear --;
    }

/// Object date for this month's date.
    $dayofmonth = 1; // returns today's date
    $dt =& new Date($year . "-" . sprintf("%02d", $month) . "-01");

    $today =& new Date();
    $dayofweek = $dt->getDayOfWeek(); // returns the day of week sunday etc.

/// Object date for prev month's date.
    if($month == 1)
      $pmonth = 31;
    else
      $pmonth = $month - 1;

    $dt_prev =& new Date($year . "-" . sprintf("%02d", $pmonth) . "-01");

    $firstweekd = $dayofmonth % 7; //6
    $diff_from_1 = $firstweekd - 1; // 5
    $actualday = $dayofweek - $diff_from_1; //-3

    if ($actualday < 0)
    $actualday = $actualday + 7;

    //Note: $actualday returns the day of 1st day of the current month.

    $days_in_month = $dt->getDaysInMonth();
    $days_in_pmonth = $dt_prev->getDaysInMonth();


    $sunday= getSunday($actualday,$month,$year);

    //Note: $sundat returns the date of sunday previous to first of each month.
?>
<table style="border: 0px solid; width: 100%;" class="menu_table">
<tr>
<td style="text-align:left; width=10%;">
<a href="<?= $_SERVER['PHP_SELF'] ?>?month=<?= $prevMonth ?>&year=<?= $prevYear ?>" title="Previous Month">&lt;&lt;</a></td>
<td style="text-align:center; width=80%;"><b><?= $Months[$month] ?>&nbsp;<?= $year ?></b></td>
<td style="text-align:right; width=10%;"><a href="<?= $_SERVER["PHP_SELF"] ?>?month=<?= $nextMonth ?>&year=<?= $nextYear ?>" title="Next Month">&gt;&gt;</a></td>
</tr>
 <hr>
 <table class="month_table" cellspacing="0" cellpadding="0" style="width: 100%; border: 1px solid rgb(200,200,200);">
        <tr>
            <th style="background-color: #eeeeee;">Sunday</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wednesday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th style="background-color: #eeeeee;">Saturday</th>
       </tr>
<?php
    $p= 0;
    $date = 1;
    $inPrevMonth = true;
    for ($i = 0;$i < 6 ;$i++) {
?>
        <tr>
<?php
           for ($j =0; $j < 7; $j++) {


               if($p >= $actualday && $date <= $days_in_month) {
                    $totalexp = getsumExpforDate($year,$month,$date);

                    $inPrevMonth = false;
                    if($date == $today->getDay() && $month == $today->getMonth() && $year == $today->getYear()) {
                        echo '<td class="today" onclick="showExp(' . $year . ', ' . $month . ', ' . $date . ');">';
                    }
                    else {
			    echo '<td ';
			    if($j == 0 || $j == 6) {
				    // shade saturday and sunday
				    echo 'style="background-color: #eeeeee;" ';
			    }
			    echo 'onclick="showExp(' . $year . ', ' . $month . ', ' . $date . ');">';
                    }

?>                  <?=$date?>

                   <? if(!empty($totalexp))
                      echo "$".sprintf("%.02f",$totalexp); ?>

                   </td>

<?php              $date ++;
                } /// for printing previous month's days.
                else  {
                    $total = $days_in_month + $days_in_pmonth;

                    if($sunday >= $total)
                        $sunday = 1;



                    if(!$inPrevMonth) {
                        $totalexp = getsumExpforDate($nextYear,$nextMonth,$sunday);
                        echo '<td class="nextmonth" onclick="showExp(' . $nextYear . ', ' . $nextMonth . ', ' . $sunday . ');">';
                    }
                    else {
                        $totalexp = getsumExpforDate($prevYear,$prevMonth,$sunday);
                        echo '<td class="lastmonth" onclick="showExp(' . $prevYear . ', ' . $prevMonth . ', ' . $sunday . ');">';
                    }
?>
                    <?=$sunday?>

                   <?php if(!empty($totalexp))
                      echo "$".sprintf("%.02f",$totalexp); ?>

                    </td>
<?php
                }
                $p++;
                $sunday++;
           }
?>
            </tr>
<?php
    }
?>
</table>
<div align="right"><a href="showgraph.php?type=cat&duration=monthly&year=<?= $year ?>&month=<?= $month ?>" title="View Graphs">View Graphs</a></div>
</body>
</html>
